//
//  LRXBringingCell.m
//  路人侠
//
//  Created by zhangxulong on 3/15/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXBringingCell.h"


#define kRotationRadian  90.0/360.0
#define kVelocity        100

@interface LRXBringingCell()

@property (nonatomic,strong) UILabel * thingLabel;
@property (nonatomic,strong) UILabel * tipLabel;
@property (nonatomic,strong) UILabel * remainTip1;
@property (nonatomic,strong) UILabel * remainTip2;

@property(strong, nonatomic) UIPanGestureRecognizer *panGestureRecognizer;
@property(assign, nonatomic) CGPoint currentPoint;
@property(assign, nonatomic) CGPoint previousPoint;
@property(assign, nonatomic) float offsetRate;
@property (nonatomic,strong) UILabel * addrLabel;
@property (nonatomic,strong) zxlCircleView * spot;
@property (nonatomic,assign) CGFloat  maxY;


@end
@implementation LRXBringingCell
- (UIImageView *)userView{
    if (_userView == nil) _userView = [[UIImageView alloc]init];
    return _userView;
}
- (UIButton *)jiedan
{
    if (_jiedan == nil) {
        _jiedan = [UIButton buttonWithType:UIButtonTypeSystem];
    }
    return _jiedan;
}
- (UILabel *)thingLabel
{
    if (_thingLabel == nil) {
        _thingLabel = [[UILabel alloc]init];
    }
    return _thingLabel;
}
- (UILabel *)tipLabel
{
    if (_tipLabel == nil) {
        _tipLabel = [[UILabel alloc]init];
    }
    return _tipLabel;
}
-(UILabel *)remainTip1{
    if (_remainTip1 == nil) {
        _remainTip1 = [[UILabel alloc]init];
    }
    return _remainTip1;
}
- (UILabel *)remainTip2{
    if (_remainTip2 == nil) {
        _remainTip2 = [[UILabel alloc]init];
    }
    return _remainTip2;
}
- (UILabel *)addrLabel
{
    if (_addrLabel == nil) {
        _addrLabel = [[UILabel alloc]init];
    }
    return _addrLabel;
}
- (zxlCircleView *)spot
{
    if (_spot == nil) {
        _spot = [[zxlCircleView alloc]init];
    }
    return _spot;
}
- (void)layoutSubviews:(LRXHelpMeInfoModel *)model
{
    
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor colorWithRed:229 green:229 blue:229];
    lineView.frame = CGRectMake(95 * hScale, 0 * hScale, self.frame.size.width - 95 * hScale, 3 * hScale);
    [self addSubview:lineView];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    NSString * imageStr = model.userIcon;
    if (![imageStr isEqual:[NSNull null]] && imageStr.length > 0)
    {
        imageStr = [routeImageUrl stringByAppendingString:imageStr];
    }else
    {
        imageStr = @"";
    }
    
    //转UTF8 编码
    imageStr = [imageStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSURL * imageUrl = [NSURL URLWithString: imageStr];
    [self.userView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"de"]];
    
    self.userView.frame = CGRectMake(16 * wScale, 30 * hScale, 60 * wScale, 60 * wScale);
    self.userView.layer.cornerRadius = self.userView.frame.size.width * 0.5;
    self.userView.layer.masksToBounds = YES;
    self.model = model;
    
    CGFloat thingLabelX = CGRectGetMaxX(self.userView.frame) + 23 * wScale;
    CGFloat thinglabelY = 38 * hScale;
    
    self.thingLabel.text = [NSString stringWithFormat:@"%ld件商品(%@元)",model.thingArray.count,model.allMoney];
    self.thingLabel.font = [UIFont systemFontOfSize:28 * hScale];
    self.thingLabel.frame = CGRectMake(thingLabelX, thinglabelY, 0, 0);
    [self.thingLabel sizeToFit];
    
    self.tipLabel.text = [NSString stringWithFormat:@" %@ ",model.tip];
    
    self.tipLabel.font = [UIFont systemFontOfSize:40 * hScale];
    self.tipLabel.textColor = [UIColor colorWithRed:255 green:161 blue:11];
    self.remainTip1.text = @"小费";
    self.remainTip1.font = [UIFont systemFontOfSize:26 * hScale];
    self.remainTip2.text = @"元";
    self.remainTip2.font = [UIFont systemFontOfSize:26 * hScale];
    
    CGFloat remainTip1X = CGRectGetMaxX(self.userView.frame) + 350 * wScale;
    self.remainTip1.frame = CGRectMake(remainTip1X, thinglabelY, 0, 0);
    [self.remainTip1 sizeToFit];
    
    self.tipLabel.frame = CGRectMake(CGRectGetMaxX(self.remainTip1.frame), 30 * hScale, 0, 0);
    [self.tipLabel sizeToFit];
    
    
    self.remainTip2.frame = CGRectMake(CGRectGetMaxX(self.tipLabel.frame), thinglabelY, 0, 0);
    [self.remainTip2 sizeToFit];
    
    if (model.back_position) {
        self.addrLabel.text = model.back_position;
    }
    if (model.sendAddr && !model.back_position) {
        self.addrLabel.text = model.sendAddr;
    }
    
    
    self.addrLabel.font = [UIFont systemFontOfSize:24 * hScale];
    self.addrLabel.frame = CGRectMake(135 * wScale, CGRectGetMaxY(self.thingLabel.frame) + 42 * hScale, 500 * wScale, 24 * hScale);
    self.addrLabel.textAlignment = NSTextAlignmentLeft;
    self.addrLabel.textColor = [UIColor colorWithRed:115 green:115 blue:115];
    
    zxlCircleView * endSpot = [[zxlCircleView alloc]init];
    endSpot.imageName = @"stSpot";
    endSpot.backgroundColor = [UIColor clearColor];
    endSpot.frame = CGRectMake(0,0, 17 * hScale, 15 * hScale);
    endSpot.center = CGPointMake(108 * wScale, self.addrLabel.center.y);
    
    self.maxY = CGRectGetMaxY(self.addrLabel.frame);
 
    
    [self addSubview:self.addrLabel];
    [self addSubview:endSpot];
    [self addSubview:self.remainTip1];
    [self addSubview:self.tipLabel];
    [self addSubview:self.remainTip2];
    [self addSubview:self.thingLabel];
    [self addSubview:self.userView];
    
    [self setTableVie];
    [self addPanGestureRecognizer];
    
    
    [self bringSubviewToFront:self.jiedan];
}

- (void)setTableVie
{
    CGFloat thingH = 0 * hScale;
    UIView * tableView = [[UIView alloc]init];
    CGFloat jiedanBtnX = 0.0;
    
    self.tableView = tableView;
    for (LRXThing * thing in [self.model thingArray]) {
        UILabel * thingName = [[UILabel alloc]init];
        UILabel * thingMoney = [[UILabel alloc]init];
        thingMoney.textAlignment = NSTextAlignmentRight;
        
        thingName.font = [UIFont systemFontOfSize:26 * hScale];
        thingMoney.font = [UIFont systemFontOfSize:26 * hScale];
        
        thingMoney.textColor = [UIColor colorWithRed:115 green:115 blue:115];
        thingName.textColor = thingMoney.textColor;
        thingName.text = [NSString stringWithFormat:@"%@%@",thing.thingName,[thing.thingCount integerValue] > 0 ? [NSString stringWithFormat:@"x%@",thing.thingCount]: @""];
        
        
        thingMoney.text = [thing.thingMoney stringByAppendingString: @"元"];
    
        thingName.frame = CGRectMake(76 * wScale, thingH, 0, 0);
        [thingName sizeToFit];
        
        //右对齐
        thingMoney.frame = CGRectMake(420 * wScale, thingH,140 * wScale, 26 * hScale);
        
        thingH = CGRectGetMaxY(thingMoney.frame);
        thingH += 46 * hScale;

        [tableView addSubview:thingMoney];
        [tableView addSubview:thingName];
        
        jiedanBtnX = CGRectGetMaxX(thingMoney.frame);
    }
    tableView.frame = CGRectMake(CGRectGetMinX(self.spot.frame), self.maxY + 45 * hScale, self.frame.size.width, thingH);
    
    
    self.jiedan.backgroundColor = mainColor;
    [self.jiedan setTitle:@"接单" forState:UIControlStateNormal];
    [self.jiedan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.jiedan.layer.cornerRadius = 7 * hScale;
    
    CGFloat jiedanBtnY = CGRectGetMaxY(tableView.frame);
    self.jiedan.frame = CGRectMake(jiedanBtnX - 130 * wScale , jiedanBtnY ,130 * wScale , 57 * hScale);
    
    [self.jiedan addTarget:self action:@selector(accpetOrder) forControlEvents:UIControlEventTouchUpInside];
   
   
    [self addSubview:self.jiedan];
    
    
    self.cellHeight = CGRectGetMaxY(self.jiedan.frame) + 10 * hScale ;
    
    [self addSubview:tableView];
}
- (void)accpetOrder
{
    if ([self.deleteCellDelegate respondsToSelector:@selector(accpetBtnClick:)]) {
        [self.deleteCellDelegate accpetBtnClick:self];
    }
}

- (void)addPanGestureRecognizer
{
    _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(slideToDeleteCell:)];
    _panGestureRecognizer.delegate = self;
    [self addGestureRecognizer:_panGestureRecognizer];

}


#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer{
    
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        
        //velocityInView 测手势 的速度
        CGPoint velocityPoint = [gestureRecognizer velocityInView:self];
        if (fabs(velocityPoint.x) > kVelocity) {
            return YES;
        }else
            return NO;
    }else
        return NO;
    
}
-(void)slideToDeleteCell:(UIPanGestureRecognizer *)panGestureRecognizer{
    
    _previousPoint = [panGestureRecognizer locationInView:self.superview];
    
    static CGPoint originalCenter;
    UIGestureRecognizerState state = panGestureRecognizer.state;
    //开始的状态
    if (state == UIGestureRecognizerStateBegan) {
        
        originalCenter = self.center;
        [self.superview bringSubviewToFront:self];
        
    }//手势改变的时候
    else if (state == UIGestureRecognizerStateChanged)
    {
        CGPoint diff = CGPointMake(_previousPoint.x - _currentPoint.x, _previousPoint.y - _currentPoint.y);
        [self handleOffset:diff];
        
    }//手势结束
    else if (state == UIGestureRecognizerStateEnded)
    {
        if (_offsetRate < 0.5) {
            [UIView animateWithDuration:0.2 animations:^{
                
                self.transform = CGAffineTransformIdentity;
                self.center = originalCenter;
                self.alpha = 1.0;
                
            }];
        }
        else
        {
            [UIView animateWithDuration:0.2 animations:^{
                self.center = CGPointMake(self.center.x * 2, self.center.y);
                self.alpha = 0.0;
                
            } completion:^(BOOL finsh){
                if ([self.deleteCellDelegate respondsToSelector:@selector(slideToDeleteCell:)]) {
                    [self.deleteCellDelegate slideToDeleteCell:self];
                }
                
                
            }];
        }
    }
    _currentPoint = _previousPoint;
    
}

//设置cell的移动的位置
-(void)handleOffset:(CGPoint)offset{
    
    self.center = CGPointMake(self.center.x + offset.x, self.center.y);
    float distance = self.frame.size.width / 2 - self.center.x;
    float distanceAbs = fabsf(distance);
    float distanceRate = (self.frame.size.width - distanceAbs) / self.frame.size.width;
    
    //设置透明度的渐变
    self.alpha = distanceRate;
    
    _offsetRate = 1 - distanceRate;
    
    if (distance >= 0) {
        self.transform = CGAffineTransformMakeRotation(-_offsetRate * kRotationRadian);
    }else
        self.transform = CGAffineTransformMakeRotation(_offsetRate * kRotationRadian);
    
}



@end
